package cn.iocoder.yudao.module.tms.controller.admin.training;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.tms.controller.admin.training.vo.*;
import cn.iocoder.yudao.module.tms.service.training.TrainingMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 培训资料")
@RestController
@RequestMapping("/tms/training-material")
@Validated
@Slf4j
public class TrainingMaterialController {

    @Resource
    private TrainingMaterialService trainingMaterialService;
    
    @Resource
    private FileApi fileApi;

    @PostMapping("/create")
    @Operation(summary = "创建培训资料")
    @PreAuthorize("@ss.hasPermission('tms:training-material:create')")
    public CommonResult<Long> createTrainingMaterial(@Valid @RequestBody TrainingMaterialSaveReqVO createReqVO) {
        return success(trainingMaterialService.createTrainingMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新培训资料")
    @PreAuthorize("@ss.hasPermission('tms:training-material:update')")
    public CommonResult<Boolean> updateTrainingMaterial(@Valid @RequestBody TrainingMaterialSaveReqVO updateReqVO) {
        trainingMaterialService.updateTrainingMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除培训资料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tms:training-material:delete')")
    public CommonResult<Boolean> deleteTrainingMaterial(@RequestParam("id") Long id) {
        trainingMaterialService.deleteTrainingMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得培训资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tms:training-material:query')")
    public CommonResult<TrainingMaterialRespVO> getTrainingMaterial(@RequestParam("id") Long id) {
        return success(trainingMaterialService.getTrainingMaterial(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得培训资料分页")
    @PreAuthorize("@ss.hasPermission('tms:training-material:query')")
    public CommonResult<PageResult<TrainingMaterialRespVO>> getTrainingMaterialPage(@Valid TrainingMaterialPageReqVO pageVO) {
        return success(trainingMaterialService.getTrainingMaterialPage(pageVO));
    }
    
    @PostMapping("/upload")
    @Operation(summary = "上传培训资料")
    @PreAuthorize("@ss.hasPermission('tms:training-material:upload')")
    public CommonResult<String> uploadTrainingMaterial(@Valid TrainingMaterialUploadReqVO uploadReqVO) throws Exception {
        MultipartFile file = uploadReqVO.getFile();
        String path = fileApi.createFile(IoUtil.readBytes(file.getInputStream()), 
            file.getOriginalFilename(), "training", file.getContentType());
        return success(path);
    }

}